package com.zzu.chat.util;

import com.zzu.chat.bean.Article;
import com.zzu.chat.bean.Blink;
import com.zzu.chat.bean.Comment;
import com.zzu.chat.bean.SubComment;
import com.zzu.chat.bean.Task;
import com.zzu.chat.bean.User;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import cn.leancloud.AVObject;
import cn.leancloud.AVUser;

public class TimeUtils {



    public static User getUserClass(){

        AVUser avuser = AVUser.getCurrentUser();
        if (avuser != null) {
            User user = new User();

            user.setId(avuser.getObjectId());
            user.setHeadImagePath(avuser.getString("headImagePath"));
            user.setUsername(avuser.getString("username"));
            user.setMobilePhone(avuser.getMobilePhoneNumber());
            user.setIntroduction(avuser.getString("introduction"));
            user.setEmail(avuser.getEmail());
            user.setAvImageFile(avuser.getAVFile("avImageFile"));

            return user;

        } else {
            return null;

        }
    }

    public static Blink getBlinkClass(AVObject object){
        Blink blink = new Blink();
        blink.setUsername(object.getString("username"));
        blink.setContent(object.getString("content"));
        blink.setTopic(object.getString("topic"));
        blink.setObjectId(object.getObjectId());
        blink.setCreatedAt(object.getCreatedAt());
        blink.setUpdatedAt(object.getUpdatedAt());
        return blink;
    }

    public static Comment getCommentClass(AVObject object){
        Comment comment = new Comment();
        comment.setHeadImageUrl(object.getString("headImageUrl"));
        comment.setUsername(object.getString("username"));
        comment.setCommentContent(object.getString("commentContent"));

        comment.setId(object.getObjectId());
        comment.setCreatedAt(object.getCreatedAt());
        comment.setUpdatedAt(object.getUpdatedAt());
        return comment;
    }
    public static SubComment getSubCommentClass(AVObject object){
        SubComment subcomment = new SubComment();
        subcomment.setContent(object.getString("content"));
        subcomment.setParent_comment_id(object.getString("parent_comment_id"));
        subcomment.setUsername(object.getString("username"));
        subcomment.setUserid(object.getString("userid"));


        return subcomment;
    }
    public static Article getArticleClass(AVObject object){
       Article article = new Article();
       article.setComment(object.getInt("comment"));
       article.setDetailUrl(object.getString("detailURL"));
       article.setImageUrl(object.getString("imageURL"));
       article.setPraise(object.getInt("praise"));
       article.setRead(object.getInt("read"));
       article.setSummary(object.getString("summary"));
       article.setTag(object.getString("tag"));
       article.setTitle(object.getString("title"));
       article.setUserName(object.getString("username"));

       return article;
    }

    public static Task getTaskClass(AVObject object){
        Task task = new Task();
        task.setObjectId(object.getObjectId());
        task.setFinishedDate(object.getDate("finishedDate"));
        task.setFinished(object.getBoolean("isFinished"));
        task.setPriority(object.getInt("priority"));
        task.setLabel(object.getString("label"));
        task.setTaskContent(object.getString("taskContent"));
        task.setFinishedTime(object.getString("finishedTime"));
        task.setFinishedMonth(object.getString("finishedMonth"));
        task.setEndTime(object.getString("endTime"));
        return task;
    }

    /**
     * 获取当前日期
     */
    public static Date getNowDateWithOutHour() throws ParseException {
         int mYear;
         int mMonth;
         int mDay;
//         int mHour;
//         int mMinute;
//         int mSecond;
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mYear = c.get(Calendar.YEAR);//年
        mMonth = c.get(Calendar.MONTH) + 1;//月
        mDay = c.get(Calendar.DAY_OF_MONTH);//日

//        mHour = c.get(Calendar.HOUR_OF_DAY);//24小时格式    HOUR(12小时格式)
//        mMinute = c.get(Calendar.MINUTE);//分
//        mSecond = c.get(Calendar.SECOND);//秒

       String s = mYear+"-"+mMonth+"-"+mDay;
       return getDateFromStringWithoutHour(s);


    }

    /**
     * 字符串转date 不带时分秒
     */
    public static Date getDateFromStringWithoutHour(String s) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(s);
        return date;
    }
    /**
     * 字符串转date 带时分秒
     */
    public static Date getDateFromStringWithHour(String s) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = dateFormat.parse(s);
        return date;
    }

    /**
     * 毫秒转字符串
     */
    public static String getStringFromDateLong(long dateLong){
        SimpleDateFormat sdf =new SimpleDateFormat("MM-dd");
        return sdf.format(dateLong);
    }

    // 获取今天的日期
    public static Date getNow() throws ParseException {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        int mYear = c.get(Calendar.YEAR);//年
        int mMonth = c.get(Calendar.MONTH) + 1;//月
        int mDay = c.get(Calendar.DAY_OF_MONTH);//日

        String s = mYear + "-" + mMonth + "-" + mDay;

        Date date = getDateFromStringWithoutHour(s);

        return date;
    }
}
